/*
* This file is part of RoboDS (http://code.google.com/p/robods/)
* Copyright (C) 2007 Blakharaz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#include "DsTile.h"

#include "SpriteManager.h"

namespace robo
{

    DsTile::DsTile(unsigned int x, unsigned int y)
        : AbstractTile(x, y)
    {
    }

    DsTile::~DsTile()
    {
    }

    void DsTile::update()
    {
        static int TILES_PER_LINE = 8; // number of 16x16 tiles per line
        static int LINE_MUL = TILES_PER_LINE*2;
	    static int LINE_MUL_FIRST_LINE = TILES_PER_LINE*2+1; 

        int* tileCoords = getTileNumber();
        int topLeftTileNum, bottomLeftTileNum;
    	
	    if (tileCoords[1] == 0) ///@todo: this is a hack, in the first line there is an additional all-black tile
	    {
		    topLeftTileNum = 2*tileCoords[0];
		    bottomLeftTileNum = 2*tileCoords[0] + LINE_MUL_FIRST_LINE;
	    }
	    else
	    {
		    topLeftTileNum = 2*tileCoords[0] + LINE_MUL + (2*tileCoords[1]-1)*LINE_MUL;
		    bottomLeftTileNum = 2*tileCoords[0] + LINE_MUL + (2*tileCoords[1])*LINE_MUL;
	    }

	    PA_SetRotMapTile(1, SpriteManager::BACKGROUND_MAP, 2 * mPositionX, 2 * mPositionY, topLeftTileNum);
        PA_SetRotMapTile(1, SpriteManager::BACKGROUND_MAP, 2 * mPositionX + 1, 2 * mPositionY, topLeftTileNum+1);
        PA_SetRotMapTile(1, SpriteManager::BACKGROUND_MAP, 2 * mPositionX, 2 * mPositionY + 1, bottomLeftTileNum);
        PA_SetRotMapTile(1, SpriteManager::BACKGROUND_MAP, 2 * mPositionX + 1, 2 * mPositionY + 1, bottomLeftTileNum+1);
    }
}
